package com.kitty.game.zhenbao;

import com.alibaba.fastjson.JSON;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.zhenbao.message.*;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

@Controller
public class ZhenbaoController {

    @Autowired
    private ZhenbaoService zhenbaoService;

    @RequestMapping
    public void openZhenbao(IoSession session, ReqGoldStallOpenMy reqGoldStallOpenMy) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }
        zhenbaoService.sendGoodsList(role);
    }

    @RequestMapping
    public void reqCashGoodsList(IoSession session, ReqStallCashGoodsList req) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }

        zhenbaoService.queryCashList(role);
    }

    @RequestMapping
    public void reqQueryCashPrice(IoSession session, ReqGoldStallCashPrice req) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }

        ResGoldStallCashPrice response = new ResGoldStallCashPrice();
        response.setName(req.getGoodsName());
        Map<Integer, Integer> prices = new HashMap<>();

        double price = req.getGoodsName() * 1.0d;
        prices.put(80, (int) (price / 10000d * 0.8));
        prices.put(90, (int) (price / 10000d * 0.9));
        prices.put(100, (int) (price / 10000d));
        prices.put(110, (int) (price / 10000d * 1.1));
        prices.put(120, (int) (price / 10000d * 1.2));
        response.setPrices(JSON.toJSONString(prices));
//        response.setPrices(JsonUtils.object2String(prices, new TypeReference<HashMap<Integer, Integer>>(){}));
        MessagePusher.pushMessage(role, response);
    }

    @RequestMapping
    public void reqBuyCash(IoSession session, ReqStallBuyCash req) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }
        zhenbaoService.buyCash(role, req.getName(), req.getPrice());
    }

    /**
     * 珍宝交易记录
     */
    @RequestMapping
    public void openZhenbao(IoSession session, ReqGoldStallRecord req) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }
        //这里写错了。要改成最新的协议
        zhenbaoService.queryTransactionRecord(role);
    }

    /**
     * 取元宝
     */
    @RequestMapping
    public void takeGold(IoSession session, ReqGoldStallTakeCash reqGoldStallTakeCash) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }
        zhenbaoService.takeCash(role);
    }

    /**
     * 上架物品
     *
     * @param session
     * @param req
     */

    @RequestMapping
    public void upShell(IoSession session, ReqGoldStallPutGoods req) {
        Role role = SessionUtils.getRoleBySession(session);
//        RespMsg respMsg = new RespMsg();
//        respMsg.setMsg("无法上架！");
//        MessagePusher.pushMessage(role, respMsg);
//        return;

        zhenbaoService.upShell(role, req);
    }


    /**
     * 珍宝逛摊
     */
    @RequestMapping
    public void lookOver(IoSession session, ReqGoldStallOpen req) {
        Role role = SessionUtils.getRoleBySession(session);
        zhenbaoService.queryGoodsList(role, req);
    }


    /**
     * 查看珍宝信息
     */
    @RequestMapping
    public void chakanInfo(IoSession session, ReqGoldStallGoodsInfo req) {
        zhenbaoService.goodsInfo(session, req);
    }

    /**
     * 珍宝撤摊
     */
    @RequestMapping
    public void chetan(IoSession session, ReqGoldStallRemoveGoods req) {
        zhenbaoService.removeGoods(session, req);
    }

    /**
     * 珍宝改价
     */
    @RequestMapping
    public void changePrice(IoSession session, ReqGoldStallChangePrice req) {
        zhenbaoService.updatePrice(session, req);
    }

    /**
     * 珍宝购买
     */
    @RequestMapping
    public void buyGoods(IoSession session, ReqGoldStallBuyGoods req) {
        Role role = SessionUtils.getRoleBySession(session);
        zhenbaoService.buyGoods(role, Long.parseLong(req.getId()), req.getPrice());
    }
    @RequestMapping
    public void authenticateFace(IoSession session, AuthenticateFaceReq authenticateFaceReq) {
        Role role = SessionUtils.getRoleBySession(session);
        AuthenticateFace authenticateFace = new AuthenticateFace();
        MessagePusher.pushMessage(role, authenticateFace);
    }
}
